{% extends "base.html" %}
{% load i18n %}
{% load account %}
{% load bootstrap3 %}
{% load dv_filters %}
{% block head_title %}{% trans "Utilisateurs" %}{% endblock %}

{% block bootstrap3_extra_script %}
  {% include 'snippets/datetimepicker_js.html' %}
{% endblock %}

{% block content %}
    {% if filter.form %}
        <div class="modal fade bs-user-filters-modal" tabindex="-1" role="dialog" aria-labelledby="filters">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="{% trans "Fermer" %}"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title">{% trans "Filtrer la liste des utilisateurs" %}</h4>
                    </div>
                    <div class="modal-body">
                    <form method="GET" action="." class="form-inline">
                        {% bootstrap_form filter.form size='small' exclude='q' show_help=False %}

                        <div class="modal-footer">
                            {% buttons %}
                                <a href="{% url 'user-list' %}" class="btn btn-default">{% trans 'Reset' %}</a>
                                <button type="submit" class="btn btn-primary">{% trans 'Filter' %}</button>
                            {% endbuttons %}
                        </div>
                    </form>
                    </div>
                </div>
            </div>
        </div>
    {% endif %}

    <div class="hidden-print">
        {% if request.user|can:"user_view_list" %}
            {% include 'export_buttons.html' with exporturl='user-list-export' filter_querystring=filter_querystring only %}
        {% endif %}

        {% if filter.form %}
            <button type="button" class="btn btn-primary pull-right" data-toggle="modal" data-target=".bs-user-filters-modal">
                <span class="glyphicon glyphicon-filter" aria-hidden="true"></span>
                {% trans "Filtres" %}
            </button>
            {% include 'user_search_form.html' %}
        {% endif %}
    </div>

    <table class="table">
    <thead>
        <tr>
            <th>{% trans "Prénom" %}</th>
            <th>{% trans "Nom" %}</th>
            <th>{% trans "Email" %}</th>
            <th>{% trans "Natel" %}</th>
            <th>{% trans "Statut" %}</th>
            <th>{% trans "Cantons" %}</th>
            <th>{% trans "Langues" %}</th>
            <th>{% trans "Rôle" %}</th>
            <th>{# Boutons #}</th>
        </tr>
    </thead>
    <tbody>
    {% for user in users %}
        <tr>
            <td>{% if request.user|can:'user_detail_other' %}<a target="_blank" href="{% url 'user-detail' pk=user.pk %}">{{ user.first_name }}</a>{% else %}{{ user.first_name }}{% endif %}</td>
            <td>{% if request.user|can:'user_detail_other' %}<a target="_blank" href="{% url 'user-detail' pk=user.pk %}">{{ user.last_name }}{% else %}{{ user.last_name }}{% endif %}</a></td>
            <td><a href="mailto:{{ user.profile.mailtolink|urlencode }}">{{ user.email }}</a></td>
            <td>
              {% if user.profile.natel %}{{ user.profile.natel|tel_link }}{% elif user.profile.phone %}{{ user.profile.phone|tel_link }}{% endif %}
            </td>
            <td class="{{ user.profile.status_class }}">
                {{ user.profile.status_icon|safe }}
            </td>
            <td>
               {{ user.profile.affiliation_canton|canton_abbr }}{% if user.profile.activity_cantons %}, <em>{{ user.profile.activity_cantons|cantons_abbr|join:"</em>, <em>" }}</em>{% endif %}
            </td>
            <td>
               {{ user.profile.language }}{% if user.profile.languages_challenges %}, <em>{{ user.profile.languages_challenges|join:"</em>, <em>" }}</em>{% endif %}
            </td>
            <td>
                {{ user.profile.formation_icon|safe }}
                {{ user.profile.actor_icon|default:"" }}
            </td>
            <td>
                {% if not user.profile.deleted %}
                    {% if request.user == user or request.user|can:"user_edit_other" and request.user|inusercantons:user.profile.affiliation_canton %}
                    <a class="btn btn-primary btn-xs" href="{% url 'user-update' pk=user.pk %}?returnUrl={{ request.get_full_path|urlencode }}">
                        <span class="glyphicon glyphicon-pencil" aria-hidden="true" title="{% trans "Éditer" %}"></span>
                    </a>
                    {% endif %}
                    {% if user.profile.can_login %}
                        <span class="btn btn-disabled btn-xs">{{ user.profile.access_level_icon }}</span>
                    {% else %}
                        {% if request.user|can:"user_can_send_credentials" and request.user.pk != user.pk and request.user|inusercantons:user.profile.affiliation_canton %}
                        <a class="btn btn-default btn-xs" href="{% url 'user-sendcredentials' pk=user.pk %}">
                            <span class="glyphicon glyphicon-envelope" aria-hidden="true" title="{% trans "Envoyer les données de connexion" %}"></span>
                        </a>
                        {% endif %}
                    {% endif %}
                {% endif %}
            </td>
        </tr>
    {% endfor %}
    </tbody>
    </table>

    {% include 'paginator.html' with page_obj=page_obj listurl='user-list' filter_querystring=filter_querystring %}

    {% if request.user|can:"user_create" %}
    <div class="hidden-print">
        <a class="btn btn-primary pull-right" href="{% url 'user-create' %}">
            <span class="glyphicon glyphicon-user" aria-hidden="true"></span>
            {% trans "Nouvel utilisateur" %}
        </a>
    </div>
    {% endif %}

    {% if request.user|can:"user_mark_inactive" %}
    <div class="hidden-print">
        <a class="btn btn-xs btn-danger" href="{% url 'users-actions-markinactive' %}">
            <span class="glyphicon glyphicon-lock" aria-hidden="true"></span>
            {% trans "Rendre tous les moniteurs inactifs" %}
        </a>
    </div>
    {% endif %}
{% endblock %}
